package org.anupta.realm.utils.others;

import java.util.List;

import org.anupta.realm.main.Main;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

public class Config {
	public static String VERSION = "1.29.1";
	public static int TIME_OUT = (15*60*1000);//Minute
	public static int TIME_SAVE = (3*1000);//Seconds
    //Sql
    public static String DRIVER = "com.mysql.jdbc.Driver";
    public static String[] LOGIN = {
    	"jdbc:mysql://localhost/anupta",
    	"baskwo",
    	"colacoka"
    };
    public static String[] LOGIN_REALM = {
    	"jdbc:mysql://localhost/anupta_realm",
    	"baskwo",
    	"colacoka"
    };
    
    //Realm
    public static String REALM_IP = "127.0.0.1";
    public static int REALM_PORT = 444;
    //Game
    public static Integer[] GAME_GUID = {};
    //Inter
    public static String INTER_IP = "127.0.0.1";
    public static int INTER_PORT = 666;//Mouhahahahaahah (6) :D
    
    public static void readConfig(String filePath){
    	XMLConfiguration xml = new XMLConfiguration();
    	xml.setListDelimiter(';');
    	xml.setFileName(filePath);
    	try {
			xml.load();
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
    	parseXML(xml);
    }
    
	private static void parseXML(XMLConfiguration xml){
    	VERSION = xml.getString("server.version", VERSION);
    	TIME_OUT = (xml.getInt("server.timeOut", TIME_OUT)*60*1000);
    	TIME_SAVE = (xml.getInt("server.saveTime", TIME_SAVE)*1000);
    	
    	DRIVER = xml.getString("sql.driver", DRIVER);
    	LOGIN[0] = xml.getString("sql.url", LOGIN[0]);
    	LOGIN[1] = xml.getString("sql.user", LOGIN[1]);
    	LOGIN[2] = xml.getString("sql.pass", LOGIN[2]);
    	LOGIN_REALM[0] = xml.getString("sql.realm.url", LOGIN_REALM[0]);
    	LOGIN_REALM[1] = xml.getString("sql.realm.user", LOGIN_REALM[1]);
    	LOGIN_REALM[2] = xml.getString("sql.realm.pass", LOGIN_REALM[2]);
    	
    	REALM_IP = xml.getString("server.realm.ip", REALM_IP);
    	REALM_PORT = xml.getInt("server.realm.port", REALM_PORT);
    	
    	GAME_GUID = getIntegers(xml.getList("server.gameGuid"));
    	
    	INTER_IP = xml.getString("server.inter.ip", INTER_IP);
    	INTER_PORT = xml.getInt("server.inter.port", INTER_PORT);
    }
	
	public static Integer[] getIntegers(Object[] obj) {
        Integer[] j = new Integer[obj.length];
        for (int i = 0; i < obj.length; i++) {
            try {
                j[i] = Integer.parseInt(obj[i].toString());
            } catch (NumberFormatException ex) {
                Main.err.write(ex);
            }
        }
        return j;
    }

    public static Integer[] getIntegers(List<?> list) {
        return getIntegers(list.toArray());
    }
}
